package cn.edu.hbwe.book.service;

import cn.edu.hbwe.book.entity.Order;
import cn.edu.hbwe.book.utils.DbUtil;
import cn.edu.hbwe.book.entity.OrderItem;
import cn.edu.hbwe.book.entity.User;

import java.sql.SQLException;
import java.util.List;

import cn.edu.hbwe.book.dao.OrderDao;
import cn.edu.hbwe.book.dao.OrderItemDao;
import cn.edu.hbwe.book.dao.ProductDao;

public class OrderService {	
	private OrderDao odao = new OrderDao();
	private OrderItemDao oidao = new OrderItemDao();
	private ProductDao pdao = new ProductDao();
	public void addOrder(Order order)  {
		// 添加订单
		// 1.开启事务
		try {
			DbUtil.startTransaction();
			// 2.1向orders表中添加数据
			odao.addOrder(order);
			// 2.2向orderItem表中添加数据
			oidao.addOrderItem(order.getOrderItems());
			pdao.changeProductNum(order);
			DbUtil.releaseAndCloseConnection();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			try {
				DbUtil.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
	}
	// 根据用户查找订单
		public List<Order> findOrderByUser(User user) {
			List<Order> orders = null;
			try {
				// 查找出订单信息
				orders = odao.findOrderByUser(user);

			} catch (SQLException e) {
				e.printStackTrace();
			}
			return orders;
		}
		// 查找所有订单
		public List<Order> findAllOrder() {
			List<Order> orders = null;
			try {
				// 查找出订单信息
				orders = odao.findAllOrder();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			return orders;
		}
		// 多条件查询订单信息
		public List<Order> findOrderByManyCondition(String id, String receiverName) {
			List<Order> orders = null;
			try {
				orders = odao.findOrderByManyCondition(id, receiverName);
			} catch (SQLException e) {
				e.printStackTrace();
			}
			return orders;
		}
		// 根据id查找订单
		public Order findOrderById(String id) {
			Order order = null;
			try {
				order = odao.findOrderById(id);
				List<OrderItem> items = oidao.findOrderItemByOrder(order);
				order.setOrderItems(items);
			} catch (SQLException e) {
				e.printStackTrace();
			}
			return order;
		}
		//根据id删除订单 管理员删除订单
		public void delOrderById(String id) {			
			try {
				DbUtil.startTransaction();//开启事务
				oidao.delOrderItems(id);
				odao.delOrderById(id);
			} catch (SQLException e) {
				e.printStackTrace();
				try {
					DbUtil.rollback();
				} catch (SQLException e1) {
					e1.printStackTrace();
				}
			}finally{
				try {
					DbUtil.releaseAndCloseConnection();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}		
		}
		//普通用户删除订单
		public void delOrderByIdWithClient(String id) {
			try {
				DbUtil.startTransaction();//开启事务
				//从订单项中查找商品购买数量
				Order order=new Order();
				order.setId(id);
				List<OrderItem> items=oidao.findOrderItemByOrder(order);
				//修改商品数量			
				pdao.updateProductNum(items);						
				oidao.delOrderItems(id); //删除订单项
				odao.delOrderById(id); //删除订单
			} catch (SQLException e) {
				e.printStackTrace();
				try {
					DbUtil.rollback();
				} catch (SQLException e1) {
					e1.printStackTrace();
				}
			}finally{
				try {
					DbUtil.releaseAndCloseConnection();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
}
